/**
 * 2016，thor.com，all rights reserved。
 * 
 * Project：		palmmall-core
 * Filename：	ATodoServiceImpl.java
 * Created：		2016年7月30日
 * Creator：		wolv
 */
package com.thor.palmmall.rest.todo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.util.ConverterUtil;
import com.thor.palmmall.rest.todo.converter.ATaskConverter;
import com.thor.palmmall.rest.todo.converter.ATaskQueryCountConverter;
import com.thor.palmmall.rest.todo.converter.ATaskWithOperationConverter;
import com.thor.palmmall.rest.todo.converter.SQueryCountParamConverter;
import com.thor.palmmall.rest.todo.converter.STaskExecutionConverter;
import com.thor.palmmall.rest.todo.converter.STaskParamConverter;
import com.thor.palmmall.rest.todo.converter.STaskQueryParamConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SQueryResultConverter;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.todo.STask;
import com.thor.palmmall.spi.todo.STaskQueryCount;
import com.thor.palmmall.spi.todo.STaskWithOperation;
import com.thor.palmmall.spi.todo.STodoService;

/**
 * @author wolv
 *
 */
public class ATodoServiceImpl implements ATodoService {

  @Override
  public com.thor.commons.query.QueryResult<ATask> getTasks(ATaskQueryParam parameter)
      throws ThorServiceException {
    SResponse<SQueryResult<STask>> sResponse = sTodoService
        .getTasks(STaskQueryParamConverter.getInstance().convert(parameter));
    if (sResponse.isSuccess())
      return SQueryResultConverter.convert(sResponse.getBody(), ATaskConverter.getInstance());
    else
      throw new ThorServiceException(sResponse.getMessage());
  }

  @Override
  public ATaskWithOperation get(String uuid) throws ThorServiceException {
    SResponse<STaskWithOperation> sResponse = sTodoService.get(uuid);
    if (sResponse.isSuccess())
      return ATaskWithOperationConverter.getInstance().convert(sResponse.getBody());
    else
      throw new ThorServiceException(sResponse.getMessage());
  }

  @Override
  public void execute(ATaskExecution execution) throws ThorServiceException {
    SResponse<Object> sResponse = sTodoService
        .execute(STaskExecutionConverter.getInstance().convert(execution));
    if (sResponse.isSuccess() == false)
      throw new ThorServiceException(sResponse.getMessage());
  }

  @Override
  public void signIn(ATaskParam param) throws ThorServiceException {
    SResponse<Object> sResponse = sTodoService
        .signIn(STaskParamConverter.getInstance().convert(param));
    if (sResponse.isSuccess() == false)
      throw new ThorServiceException(sResponse.getMessage());
  }

  @Override
  public List<ATaskQueryCount> queryCount(AQueryCountParam param) throws ThorServiceException {
    SResponse<List<STaskQueryCount>> sResponse = sTodoService
        .queryCount(SQueryCountParamConverter.getInstance().convert(param));
    if (sResponse.isSuccess())
      return ConverterUtil.convert(sResponse.getBody(), ATaskQueryCountConverter.getInstance());
    else
      throw new ThorServiceException(sResponse.getMessage());
  }
  
  
  @Override
  public void assign(ATaskParam param) throws ThorServiceException {
    SResponse<Object> sResponse = sTodoService
        .assign(STaskParamConverter.getInstance().convert(param));
    if (sResponse.isSuccess() == false)
      throw new ThorServiceException(sResponse.getMessage());
  }


  @Override
  public void assignUser(ATaskParam param) throws ThorServiceException {
    SResponse<Object> sResponse = sTodoService
        .assignUser(STaskParamConverter.getInstance().convert(param));
    if (sResponse.isSuccess() == false)
      throw new ThorServiceException(sResponse.getMessage());
  }


  @Autowired
  private STodoService sTodoService;

}
